package com.myx.dao.comment;

import com.myx.pojo.Comment;
import com.myx.util.JdbcUtil;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CommentDaoImpl implements CommentDao{
    private final JdbcUtil jdbcUtil=new JdbcUtil();
    @Override
    public List<Comment> getCommentListByNewsId(Integer newsId) {
        String sql="select * from news_comment where newsId=? order by createDate desc";
        Object [] params={newsId};
        ResultSet resultSet = this.jdbcUtil.getObjectByParams(sql, params);
        ArrayList<Comment> commentList = new ArrayList<>();
            try {
                while (resultSet.next()) {
                    Integer id=resultSet.getInt("id");
                    Integer newsId1=resultSet.getInt("newsId");
                    String content = resultSet.getString("content");
                    Date createDate = resultSet.getDate("createDate");
                    Integer createBy = resultSet.getInt("createBy");
                    Date modifyDate = resultSet.getDate("modifyDate");
                    Integer modifyBy = resultSet.getInt("modifyBy");

                    Comment comment = new Comment();
                    comment.setId(id);
                    comment.setNewsId(newsId1);
                    comment.setContent(content);
                    comment.setCreateDate(createDate);
                    comment.setCreateBy(createBy);
                    comment.setModifyDate(modifyDate);
                    comment.setModifyBy(modifyBy);
                    commentList.add(comment);

                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                this.jdbcUtil.closeResource();
            }
        return commentList;
    }
}
